#include	"Wiimote4Virtools.h"
#include	"WiimButtons.h"

CKObjectDeclaration *FillBehaviorWiimButtons()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration ( "WiimButtons" );
	od->SetDescription ( "Gets buttons states from a Wiimote." );
	od->SetCategory ( "Wiimote" );
	od->SetType ( CKDLL_BEHAVIORPROTOTYPE );
	od->SetGuid ( WIIMBUTTONS_GUID );
	od->SetAuthorGuid ( AUTHOR_GUID );
	od->SetAuthorName ( "Jonathan DERROUGH" );
	od->SetVersion ( 0x00000001 );
	od->SetCreationFunction ( CreateWiimButtonsProto );
	//od->SetCompatibleClassId ( CKCID_3DENTITY );
	return od;
}

CKERROR CreateWiimButtonsProto ( CKBehaviorPrototype **pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("WiimButtons");

	if(!proto)
		return (CKERR_OUTOFMEMORY);

	proto->DeclareInput("In");
	
	proto->DeclareInParameter("Wiimote id", CKPGUID_INT );

	proto->DeclareOutput("None" );

	proto->DeclareOutput("Left" );
	proto->DeclareOutput("Right" );
	proto->DeclareOutput("Up" );
	proto->DeclareOutput("Down" );

	proto->DeclareOutput("A" );
	proto->DeclareOutput("B" );

	proto->DeclareOutput("Minus" );
	proto->DeclareOutput("Home" );
	proto->DeclareOutput("Plus" );

	proto->DeclareOutput("One" );
	proto->DeclareOutput("Two" );

	proto->DeclareOutParameter("Left", CKPGUID_BOOL);
	proto->DeclareOutParameter("Right", CKPGUID_BOOL);
	proto->DeclareOutParameter("Up", CKPGUID_BOOL);
	proto->DeclareOutParameter("Down", CKPGUID_BOOL);

	proto->DeclareOutParameter("A", CKPGUID_BOOL);
	proto->DeclareOutParameter("B", CKPGUID_BOOL);

	proto->DeclareOutParameter("Minus", CKPGUID_BOOL);
	proto->DeclareOutParameter("Home", CKPGUID_BOOL);
	proto->DeclareOutParameter("Plus", CKPGUID_BOOL);

	proto->DeclareOutParameter("One", CKPGUID_BOOL);
	proto->DeclareOutParameter("Two", CKPGUID_BOOL);

	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(WiimButtons);
	//proto->SetBehaviorFlags(CKBEHAVIOR_TARGETABLE);

	*pproto = proto;
	return (CK_OK);
}

int WiimButtons(const CKBehaviorContext& behcontext)
{
	CKBehavior* beh = behcontext.Behavior;

	beh->ActivateInput(0, FALSE);	// deactivation

	// Wiimote(s) present ?
	if (g_wiimotes.size() < 1)
	{
		beh->ActivateOutput(1);
		return (CKBR_OK);
	}

	// Get the id of the Wiimote we are using
	int wiimote_id = 0;
	beh->GetInputParameterValue(0, &wiimote_id);

	if (wiimote_id >= g_wiimotes.size())
	{
		beh->ActivateOutput(0);
		return (CKBR_PARAMETERERROR);
	}

	static int last_left = 0;
	static int last_right = 0;
	static int last_up = 0;
	static int last_down = 0;
	static int last_minus = 0;
	static int last_home = 0;
	static int last_plus = 0;
	static int last_one = 0;
	static int last_two = 0;
	static int last_a = 0;
	static int last_b = 0;

	Button Left = g_wiimotes[wiimote_id].GetButton("Left");
	Button Right = g_wiimotes[wiimote_id].GetButton("Right");
	Button Up = g_wiimotes[wiimote_id].GetButton("Up");
	Button Down = g_wiimotes[wiimote_id].GetButton("Down");
	Button Minus = g_wiimotes[wiimote_id].GetButton("-");
	Button Home = g_wiimotes[wiimote_id].GetButton("Home");
	Button Plus = g_wiimotes[wiimote_id].GetButton("+");
	Button One = g_wiimotes[wiimote_id].GetButton("1");
	Button Two = g_wiimotes[wiimote_id].GetButton("2");
	Button A = g_wiimotes[wiimote_id].GetButton("A");
	Button B = g_wiimotes[wiimote_id].GetButton("B");

	
	int pressed = Left.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(0, &pressed);
	pressed = Right.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(1, &pressed);
	pressed = Up.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(2, &pressed);
	pressed = Down.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(3, &pressed);
	pressed = A.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(4, &pressed);
	pressed = B.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(5, &pressed);
	pressed = Minus.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(6, &pressed);
	pressed = Home.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(7, &pressed);
	pressed = Plus.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(8, &pressed);
	pressed = One.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(9, &pressed);
	pressed = Two.Pressed() ? 1 : 0;
	beh->SetOutputParameterValue(10, &pressed);

	bool is_pressed = false;

	if (Left.Pressed() == 1 && last_left == 0)
	{	
		beh->ActivateOutput(1);
		is_pressed = true;
	}
	last_left = Left.Pressed();

	if (Right.Pressed() == 1 && last_right == 0)
	{
		beh->ActivateOutput(2);
		is_pressed = true;
	}
	last_right = Right.Pressed();

	if (Up.Pressed() == 1 && last_up == 0)
	{
		beh->ActivateOutput(3);
		is_pressed = true;
	}
	last_up = Up.Pressed();

	if (Down.Pressed() == 1 && last_down == 0)
	{
		beh->ActivateOutput(4);
		is_pressed = true;
	}
	last_down = Down.Pressed();

	if (A.Pressed() == 1 && last_a == 0)
	{
		beh->ActivateOutput(5);
		is_pressed = true;
	}
	last_a = A.Pressed();

	if (B.Pressed() == 1 && last_b == 0)
	{
		beh->ActivateOutput(6);
		is_pressed = true;
	}
	last_b = B.Pressed();

	if (Minus.Pressed() == 1 && last_minus == 0)
	{
		beh->ActivateOutput(7);
		is_pressed = true;
	}
	last_minus = Minus.Pressed();

	if (Home.Pressed() == 1 && last_home == 0)
	{
		beh->ActivateOutput(8);
		is_pressed = true;
	}
	last_home = Home.Pressed();

	if (Plus.Pressed() == 1 && last_plus == 0)
	{
		beh->ActivateOutput(9);
		is_pressed = true;
	}
	last_plus = Plus.Pressed();

	if (One.Pressed() == 1 && last_one == 0)
	{
		beh->ActivateOutput(10);
		is_pressed = true;
	}
	last_one = One.Pressed();

	if (Two.Pressed() == 1 && last_two == 0)
	{
		beh->ActivateOutput(11);
		is_pressed = true;
	}
	last_two = Two.Pressed();

	if (!is_pressed)
	{
		beh->ActivateOutput(0);
	}

	return (CKBR_OK);
}